package com.helen_dragon.healthsystem.service;/*
 *@Author: helen
 *@Date:   2021/6/6 11:23
 *@Description:
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.helen_dragon.healthsystem.mapper.informationMapper;
import com.helen_dragon.healthsystem.mapper.personMapper;
import com.helen_dragon.healthsystem.model.information;
import com.helen_dragon.healthsystem.model.person;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;

@Service
public class personService {
    @Autowired
    private personMapper perMapper;
    @Autowired
    private informationMapper infoMapper;


    public void InsertInfo(information info, String personId){
        QueryWrapper<person> wrapper = new QueryWrapper<>();
        wrapper.eq("person_id", personId);
        person per = perMapper.selectOne(wrapper);
        info.setName(per.getName());
        info.setPersonId(per.getPersonId());
        info.setTime(LocalDateTime.now());
        System.out.println(LocalDateTime.now());
        infoMapper.insert(info);
    }

    //通过学号或教职工号查出他们的账号密码
    public person selectPerson(String personId){
        QueryWrapper<person> wrapper = new QueryWrapper<>();
        wrapper.eq("person_id", personId);
        final person per = perMapper.selectOne(wrapper);
        return per;
    }

    //注册插入学号/职工号
    public int insertPerson(person per){
        person person = selectPerson(per.getPersonId());
        if(person == null) {
            int i = perMapper.insert(per);
            return i;
        }
        else
            return 0;
    }

    //判断当前账号今天是否打卡过
    public boolean is_clock_in(String personId){
        QueryWrapper<information> wrapper = new QueryWrapper<>();

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateFormat = sdf.format(date);
        //使用time进行模糊查询判断今天是否打卡过
        wrapper.eq("person_id", personId);
        wrapper.like("time", dateFormat);
        information information = infoMapper.selectOne(wrapper);
        if(information == null)
            return false;
        else return true;
    }


}
